package cl.jsoftlabs.gwt.soccer.client.mvc.views;

import cl.jsoftlabs.gwt.soccer.client.bean.AppPestana;
import cl.jsoftlabs.gwt.soccer.client.bean.JugadorVO;
import cl.jsoftlabs.gwt.soccer.client.mvc.controllers.AppController;
import cl.jsoftlabs.gwt.soccer.client.mvc.events.AppEvents;
import cl.jsoftlabs.gwt.soccer.client.ui.LoginDialog;
import cl.jsoftlabs.gwt.soccer.client.ui.home.HomePage;
import cl.jsoftlabs.gwt.soccer.client.ui.jugadores.FormularioContrato;
import cl.jsoftlabs.gwt.soccer.client.ui.jugadores.GridJugadores;
import cl.jsoftlabs.gwt.soccer.client.ui.pais.PaisListField;
import cl.jsoftlabs.gwt.soccer.client.ui.pais.PaisesGrid;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;

public class AppView extends View {

	private final ContentPanel mainPanel = new ContentPanel();
	private final Viewport viewport = new Viewport();
	private final HomePage pagina = new HomePage();

	public AppView(AppController appController) {
		super(appController);
	}

	protected void initialize() {
		LoginDialog dialog = new LoginDialog();
		dialog.setClosable(false);
		dialog.addListener(Events.Hide, new Listener<WindowEvent>() {
			public void handleEvent(WindowEvent be) {
				Dispatcher.forwardEvent(AppEvents.UIReady);
			}
		});
		dialog.show();
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType eventType = event.getType();
		if (eventType.equals(AppEvents.Init)) {
			onInit(event);
		} else if (eventType.equals(AppEvents.Error)) {
			onError(event);
		} else if (eventType.equals(AppEvents.UIReady)) {
			onUIReady(event);
		} else if (eventType.equals(AppEvents.AppNuevaPestana)) {
		 	onAppNuevaPestana(event);
		} else if (eventType.equals(AppEvents.StatusToolbarReady)) {
			onStatusToolbarReady(event);
		} else if (eventType.equals(AppEvents.mostrarTablaJugadores)) {
			onMostrarGridJugadores(event);
		}  else if (eventType.equals(AppEvents.MostrarTablaPaises)) {
		 	onMostrarTablaPaises(event);
		}  else if (eventType.equals(AppEvents.NuevoContrato)) {
		 	onNuevoContrato(event);
		}
	}

	private void onNuevoContrato(AppEvent event) {
		FormularioContrato form = new FormularioContrato();
		form.setJugador((JugadorVO) event.getData() );
		form.show();
	}

	private void onAppNuevaPestana(AppEvent event) {
		AppPestana pestana = (AppPestana) event.getData();
		pagina.addTab(pestana.getTitulo(), pestana.getElemento());
	}

	private void onMostrarTablaPaises(AppEvent event) {
		LayoutContainer contenedor = new LayoutContainer();
		String nombre = event.getData();
		final PaisesGrid itemGrid = new PaisesGrid();
		contenedor.add(itemGrid);
		pagina.addTab(nombre, itemGrid);
		//pagina.addTab("ListField", new PaisListField());
	}

	

	private void onMostrarGridJugadores(AppEvent event) {
		String nombre = event.getData();
		final GridJugadores itemGrid = new GridJugadores();
		pagina.addTab(nombre, itemGrid);
	}

	private void onUIReady(AppEvent event) {
		Info.display("Evento", "UIREADY");
		RootPanel.get().clear();
		RootPanel.get().add(viewport);
	}

	private void onError(AppEvent event) {
		Info.display("Evento", "EERROORRO");
	}

	private void onInit(AppEvent event) {
		Info.display("Evento", "ON INIT");
		viewport.setLayout(new FitLayout());
		viewport.add(pagina);
	}

	private void onStatusToolbarReady(AppEvent event) {
		Component component = event.getData();
		mainPanel.setBottomComponent(component);
	}

}
